<?php

namespace app\admin\controller\school\statistics;

use app\BaseController;
use app\model\not_del\UserModel;
use app\model\RatingScaleLogModel;
use app\model\RatingScaleModel;
use app\model\school\SchoolCourseChapterCommentModel;
use app\model\school\SchoolCourseChapterLearningModel;
use app\model\school\SchoolCourseChapterModel;
use app\model\school\SchoolCourseChapterResultModel;
use app\model\school\SchoolCourseModel;
use think\App;

class Index extends BaseController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 课程排行
     * @return bool|string
     */
    public function ranking(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require',
            ]);
            $SchoolCourseModel = new SchoolCourseModel();
            $SchoolCourseChapterModel = new SchoolCourseChapterModel();
            $course_count = $SchoolCourseModel->where('unit_id','=',$this->userData['unit_id'])->count();
            $chapter_count = $SchoolCourseChapterModel->where('unit_id','=',$this->userData['unit_id'])->count();
            $list = $SchoolCourseChapterModel->where('unit_id','=',$this->userData['unit_id'])->with([
                'course'
            ])->order('study_num','desc')->paginate($param['pageSize']);
            return success([
                'course_count' => $course_count,
                'chapter_count' => $chapter_count,
                'list' => $list
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 学习人数
     * @return bool|string
     */
    public function learningNum(): bool|string
    {
        try {
            $SchoolCourseChapterLearningModel = new SchoolCourseChapterLearningModel();
            $uid = $SchoolCourseChapterLearningModel->where('unit_id','=',$this->userData['unit_id'])->column('uid');
            if (!empty($uid)){
                $uid = array_unique($uid);
                $count = count($uid);
                $UserModel = new UserModel();
                $boy_count = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
                $girl_count = $count - $boy_count;
            }else{
                $count = 0;
                $boy_count = 0;
                $girl_count = 0;
            }
            return success([
                'count' => $count,
                'boy_count' => $boy_count,
                'girl_count' => $girl_count
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 章节测试任务报告预警统计
     * @return bool|string
     */
    public function evaluation(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页显示数量' => 'require',
                'page|当前页码' => 'require',
            ]);
            $SchoolCourseChapterResultModel = new SchoolCourseChapterResultModel();
            $uid = $SchoolCourseChapterResultModel->where('unit_id','=',$this->userData['unit_id'])->column('uid');
            $uid = array_unique($uid);
            //测评总人数
            $count = count($uid);
            $uid = $SchoolCourseChapterResultModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_warning','=',1]
            ])->column('uid');
            if (!empty($uid)){
                $uid = array_unique($uid);
                //预警总人数
                $warning = count($uid);
                $UserModel = new UserModel();
                //预警男生
                $warning_boy = $UserModel->where([
                    ['id','in',$uid],
                    ['sex','=',1]
                ])->count();
                //预警女生
                $warning_girl = $warning - $warning_boy;
            }else{
                $warning = 0;
                $warning_boy = 0;
                $warning_girl = 0;
            }
            //量表测评排行
            $RatingScaleLogModel = new RatingScaleLogModel();
            $RatingScaleModel = new RatingScaleModel();
            $log = $RatingScaleLogModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['source','=',9]
            ])->column('rating_scale_id');
            $rating_scale_id = [];
            $rating_scale_num = [];
            foreach ($log as $v){
                if (!in_array($v,$rating_scale_id)){
                    $rating_scale_id[] = $v;
                }
                if (isset($rating_scale_num[$v])){
                    $rating_scale_num[$v]++;
                }else{
                    $rating_scale_num[$v] = 1;
                }
            }
            $list = $RatingScaleModel->field('id,name')->where('id','in',$rating_scale_id)->select()->toArray();
            $field = [];
            foreach ($list as $key => &$item){
                $item['num'] = $rating_scale_num[$item['id']]??0;
                $field[$key] = $item['num'];
            }
            array_multisort($field, SORT_DESC, $list);
            $data = paginateArray($list, $param['page'], $param['pageSize']);
            return success([
                'count' => $count,
                'warning' => $warning,
                'warning_boy' => $warning_boy,
                'warning_girl' => $warning_girl,
                'list' => $data
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }


    /**
     * 使用人数
     * @return bool|string
     */
    public function use(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'type' => 'require|integer|in:1,2,3',
            ]);
            $time = match ($param['type']) {
                '1' => custom_time([], 'y'),
                '2' => custom_time([], 'm'),
                '3' => custom_time(),
                default => throw new \Exception('参数错误！'),
            };
            $data = [];
            $SchoolCourseChapterLearningModel = new SchoolCourseChapterLearningModel();
//            $SchoolCourseChapterModel = new SchoolCourseChapterModel();
            foreach ($time as $item) {
                $data['date'][] = $item['value'];
                $start_time = strtotime($item['start_time']);
                $end_time = strtotime($item['end_time']);
                $uid = $SchoolCourseChapterLearningModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['time','between',[$start_time,$end_time]]
                ])->column('uid');
                $uid = array_unique($uid);
                $data['finish'][] = count($uid);

                $data['finish2'][] = 0;
            }
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 其他
     * @return bool|string
     */
    public function other(): bool|string
    {
        try {
            $SchoolCourseChapterLearningModel = new SchoolCourseChapterLearningModel();
            $SchoolCourseChapterCommentModel = new SchoolCourseChapterCommentModel();
            $num = $SchoolCourseChapterCommentModel->where('unit_id','=',$this->userData['unit_id'])->count();
            $people_num = $SchoolCourseChapterCommentModel->where('unit_id','=',$this->userData['unit_id'])->column('uid');
            if (!empty($people_num)){
                $people_num = array_unique($people_num);
                $people_num = $SchoolCourseChapterLearningModel->where([
                    ['unit_id','=',$this->userData['unit_id']],
                    ['is_learning','=',1],
                    ['uid','in',$people_num]
                ])->column('uid');
                $people_num = count(array_unique($people_num));
            }else{
                $people_num = 0;
            }
            //已学习人数
            $learning_num = $SchoolCourseChapterLearningModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['is_learning','=',1]
            ])->column('uid');
            $learning_num = count(array_unique($learning_num));
            return success([
                'people_num' => $people_num,
                'num' => $num,
                'learning_num' => $learning_num
            ]);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}